import nodeResolve from "@rollup/plugin-node-resolve";
import fs, { outputFileSync } from "fs-extra";
import commonjs from "@rollup/plugin-commonjs";
import analyze from "rollup-plugin-analyzer";

// ! 不要妄想使用 importsmap 导入 @codingame/monaco-editor 或者是其他库，这些库没有后缀名，属于 nodejs 代码文件

fs.emptyDir("./dist/features/");
// rollup.config.js
export default {
    external: [],
    input: "./lib/features.js",
    output: {
        dir: "./dist/features",
        format: "es",
    },
    plugins: [
        {
            resolveDynamicImport(thisFile, importer) {
                if (importer.includes("extension")) {
                    //* extension 内部有很多异步导入，均为简单模块
                    return {
                        external: true,
                        id: new URL(
                            thisFile,
                            "https://cdn.jsdelivr.net/npm/@codingame/monaco-editor-wrapper@2.2.1/dist/features/index.js"
                        ).toString(),
                    };
                }
            },
            resolveId(thisFile, importer) {
                if (thisFile === "../index.js") {
                    return { external: true, id: "../wrapper/wrapper.js" };
                }
                if (thisFile === "../worker.js") {
                    return {
                        external: true,
                        id: "https://cdn.jsdelivr.net/npm/@codingame/monaco-editor-wrapper@1.15.0/dist/worker.js",
                    };
                }

                if (["monaco-editor"].some((i) => thisFile.startsWith(i))) {
                    const id = thisFile.replace(
                        "monaco-editor/esm/vs",
                        "../editor"
                    );
                    return {
                        external: true,
                        id: id.endsWith(".js") ? id : id + ".js",
                    };
                } // 更改 worker 指向
                if (thisFile.endsWith(".worker.js")) {
                    console.log("worker change", thisFile);
                    return {
                        external: "true",
                        id: thisFile.replace(
                            "../",
                            "https://cdn.jsdelivr.net/npm/@codingame/monaco-editor-wrapper@2.2.1/dist/"
                        ),
                    };
                }
                if (thisFile.startsWith("https://")) {
                    return false;
                }

                if (!/^[[\w:]|\.|\/]/.test(thisFile)) console.log(thisFile);
            },
            transform(code, id) {
                // 这个 文件导入了 editor 产物，但是却没有指定好导入名称
                //* import { r as registerConfigurations, g as getConfiguration, o as onConfigurationChanged } from '../index.js';
                if (id.endsWith("dist\\features\\htmlContribution.js")) {
                    console.log("fixed htmlContribution export");
                    return code.replace(
                        /import \{(.*?)\} from '..\/index\.js';/,
                        (_, $1) => {
                            const res = `import {${$1.replace(
                                /\w+ as /g,
                                ""
                            )} } from '../index.js';`;
                            return res;
                        }
                    );
                }
            },
        },

        nodeResolve({
            extensions: [".ts", ".tsx", ".js"],
        }),
        commonjs(),

        // babel({
        //     babelHelpers: "bundled",
        //     extensions: [".ts", ".tsx", ".js", ".svg"],
        //     exclude: ["**/@codingame/**/*"],
        // }),
        analyze({
            summaryOnly: true,
            writeTo: (str) =>
                outputFileSync("dist/analyze/features.analyze.txt", str),
        }),
    ],
};
